package cn.edu.jxau.test;

import cn.edu.jxau.util.LinkedStack;
import cn.edu.jxau.util.Stack;

/**
 * 中序表达式转后序表达式
 * @author 付大石
 */
public class Main {

    public static void main(String[] args) {
        
        String[] infixExp = {"(","(","1","+","2",")","*","(","(","3","-","4",")","*","(","5","-","6",")",")",")"};
        Stack<String> operationStack = new LinkedStack<>();
        StringBuilder postfixExp = new StringBuilder();
        for(String item : infixExp) {
            if(item.equals("(")) {
                //nothing to do
            } else if(item.equals("+")) {
                operationStack.push(item);
            } else if(item.equals("-")) {
                operationStack.push(item);
            } else if(item.equals("*")) {
                operationStack.push(item);
            } else if(item.equals("/")) {
                operationStack.push(item);
            } else if(item.equals(")")) {
                postfixExp.append(operationStack.pop()).append(" ");
            } else {
                postfixExp.append(item).append(" ");
            }
        }
        System.out.println(postfixExp.toString());
    }
}
